package com.aaa.eat.service.Impl;

import com.aaa.eat.mapper.RegisterMapper;
import com.aaa.eat.pojo.vo.Yonghu;
import com.aaa.eat.result.AjaxResult;
import com.aaa.eat.service.RegisterService;
import com.aaa.eat.syscount.LoginCount;
import com.aaa.eat.util.SendCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * Author:张海金
 * Description:T0D0
 * DateTime:2022/2/5 18:25
 **/
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private HttpSession session;
    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public AjaxResult sendCode(String telephone) {
        String code = SendCodeUtil.sendCode(telephone);

        System.out.println(code);
        session.setAttribute(telephone,code);
        return AjaxResult.success();
    }



    @Override
    public AjaxResult add(Yonghu yonghu) {
        System.out.println(yonghu);



        String telephone = yonghu.getTelephone();
        Integer code = yonghu.getCode();
        System.out.println(code);
        Object attribute = session.getAttribute(telephone);
        System.out.println("session验证码" + attribute);

        if(attribute == null){
            return AjaxResult.fail("短信已失效");
        }

            yonghu.setState(0);
            int i = registerMapper.addRegister(yonghu);
            if( i>0 ){
                return AjaxResult.success();
            }
            return AjaxResult.success();


    }


}
